package com.gitee.aurora.agent.dao.impl;

import com.balloon.springboot.datasource.aop.DataSourceRouteKey;
import com.gitee.aurora.agent.dao.BaqCheckDao;
import com.gitee.aurora.agent.vo.DataCheckVo;
import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import com.gitee.aurora.core.comm.AuroraNamedParameterJdbcTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: jiangfuxiao
 * Date:2022/9/2 11:47
 */
@Repository("baqCheckDaoImpl")
class BaqCheckDaoImpl implements BaqCheckDao {

    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;

    @Autowired
    private AuroraNamedParameterJdbcTemplate jdbcTemplate;


    @Override
    public List<Map<String, Object>> queryTableData(DataCheckVo vo) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Object>  paramList = new ArrayList<>();
        Map<String,Object> args = new HashMap<>();
        int[] types = new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR};
        try {
            StringBuilder sql = new StringBuilder(vo.getSqlStr());
            if (!CollectionUtils.isEmpty(vo.getIds())){
                args.put("ids",vo.getIds());
                sql.append("  AND T1.S_ID IN (:ids) ");
                sql.append("  GROUP BY T1.S_ID ");
                sql.append("  ORDER BY T1.DT_UPDATE_TIME DESC ");
                list = jdbcTemplate.queryForList(sql.toString(),args);
                return  list;
            }
            if (!"null".equals(vo.getStartTime()) && StringUtils.isNotBlank(vo.getStartTime())){
                sql.append("  AND T1.DT_UPDATE_TIME >=  ? ");
                paramList.add(vo.getStartTime());
            }
            if (!"null".equals(vo.getEndTime()) && StringUtils.isNotBlank(vo.getEndTime())){
                sql.append("  AND T1.DT_UPDATE_TIME <=  ? ");
                paramList.add(vo.getEndTime());
            }
            sql.append("  GROUP BY T1.S_ID ");
            sql.append("  ORDER BY T1.DT_UPDATE_TIME DESC ");


            list = auroraJdbcTemplate.queryForList(sql.toString(), paramList.toArray(),types);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<String> queryTableUpdateDate(DataCheckVo vo) {
        List<String> list = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        builder.append(" SELECT  DATE_FORMAT(MAX(a.DT_UPDATE_TIME),\"%Y-%m-%d %H:%i:%S\") DT_UPDATE_TIME FROM ");
        builder.append(" (    ");
        builder.append(vo.getSqlStr());
        builder.append("  )  a  ");
        try {
            list = auroraJdbcTemplate.queryForList(builder.toString(),String.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    
    @Override
    @DataSourceRouteKey(type="secondMysqlDB")
    public String queryTableConfigSql(DataCheckVo vo) {
        String sqlStr = "";
        List<Object>  paramList = new ArrayList<>();
        int[] types = new int[]{java.sql.Types.VARCHAR,java.sql.Types.VARCHAR};
        StringBuilder sql = new StringBuilder();
        sql.append("  SELECT T1.PT_SQL AS sqlStr FROM ");
        sql.append("  TAB_PT_CONFIG T1                ");
        sql.append("  WHERE 1=1                       ");
        sql.append("  AND T1.PT_CODE = 1              ");
        sql.append("  AND T1.S_CODE= ?                ");
        paramList.add(vo.getCode());
        sql.append("  AND T1.PT_TABLE= ?                ");
        paramList.add(vo.getTableName());
        try {
            sqlStr = (String)auroraJdbcTemplate.queryForList(sql.toString(), paramList.toArray(),types).get(0).get("sqlStr");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqlStr;
    }


    @Override
    @DataSourceRouteKey(type="secondMysqlDB")
    public List<Map<String, Object>> queryTable() {
        List<Map<String, Object>> list = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        builder.append(" SELECT  TABLE_CODE AS tabCode, TABLE_CODE_NAME AS tabName  FROM  TAB_TABLE_CONFIG");
        try {
            list = auroraJdbcTemplate.queryForList(builder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }


}
